﻿using AloneLibrary.GameObjects;
using AloneLibrary.Services.Camera;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Alone.GameObjects.Objects {
    class Terrain : StaticGameObject {
        
        private Texture2D grass;

        /// <summary>
        /// Initializes a new instance of the <see cref="Terrain"/> class.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="position">The position.</param>
        /// <param name="direction">The direction.</param>
        /// <param name="scale">The scale.</param>
        public Terrain(Model model, Texture2D grass, Effect effect)
            : base(model, effect) {
            this.grass = grass;
        }

        /// <summary>
        /// Draws the specified game time.
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        /// <param name="camera">The camera.</param>
        public override void Draw(GameTime gameTime, ICameraHandler camera) {

            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in this.model.Meshes) {
                foreach (ModelMeshPart part in mesh.MeshParts) {

                    part.Effect = this.effect;
                    effect.CurrentTechnique = effect.Techniques["Simplest"];
                    effect.Parameters["WorldViewProjection"].SetValue(this.world * camera.View * camera.Projection);
                    effect.Parameters["Texture"].SetValue(this.grass);
                    effect.Parameters["World"].SetValue(this.world);
                    effect.Parameters["LightPos"].SetValue(new Vector3(-8, 4, -2));
                    effect.Parameters["LightPower"].SetValue(0.5f);
                    effect.Parameters["Ambient"].SetValue(0.2f);
                    mesh.Draw();
                }
            }
        }
    }
}
